<?php
namespace Home\Controller;

class CommentController extends \Home\Controller\BaseController
{
	/**
	 * 添加评论
	 * @author 刘新
	 */
	public function addComment()
	{
		$param = I('params');
		$data['invest_id'] = $param['invest_id'];
		$data['content'] = $param['content'];
		$data['star'] = $param['star'];
		$data['user_id'] = $this->user['userId'];
		$data['parent_id'] = -1;
		$res = D('Comment')->addComment($data);
		$this->ajaxReturn($res);
	}
	
	/**
	 * 添加评论回复
	 * @author 刘新
	 */
	public function addReply()
	{
		$param = I('params');
		if(!I('data.comment_id', '', '/^\d+$/', $param)) $this->ajaxReturn(array('error' => 1, 'info' => '参数错误'));
		
		$comModel = D('Comment');
		//查询要回复的评论是否存在
		$com = $comModel->commentOne(array('id' => $param['comment_id']));
		if(!$com) $this->ajaxReturn(array('error' => 1, 'info' => '要回复的评论不存在'));
		
		//查看用户是否回复过
		$where['parent_id'] = $param['comment_id'];
		$where['user_id'] = $this->user['userId'];
		$res = $comModel->commentOne($where);
		if($res) $this->ajaxReturn(array('error' => 1, 'info' => '您已经回复过'));
		
		$data['parent_id'] = $param['comment_id'];
		$data['content'] = $param['content'];
		$data['user_id'] = $this->user['userId'];
		$data['fund_id'] = $com['fund_id'];
				
		try {
			//添加回复
			$res = $comModel->addComment($data);
			if($res['error'] !== 0) throw  new \Exception($res['info']);	
			
			//更新回复数
			$res = $comModel->where(array('id' => $param['comment_id']))->setInc('count');
			if(!$res) throw  new \Exception('回复失败1');
			$return = array('error' => 0, 'info' => '回复成功');
			$comModel->commit();
		} catch (\Exception $e) {
			$comModel->rollback();
			$return = array('error' => 1, 'info' => $e->getMessage());
		}
		
		$this->ajaxReturn($return);
	}
	
	
	/**
	 * 查询评论列表
	 * @author 刘新
	 */
	public function commentList()
	{
		$param = I('params');
	
		//组装查询条件
		$option['page'] = I('data.page', '', '/^\d+$/', $param) ? $param['page'] : 1;
		$option['size'] = I('data.size', '', '/^\d+$/', $param) ? $param['size'] : 5;
		$good_id = I('data.good_id', '', '/^\d+$/', $param);
		//查询
		$com = D('Comment')->commentList(array('good_id' => $good_id), $option);
		$returninfo = array();
		$starcount = 0;
		$count = 0;
		foreach($com as $key=>$val){
			$val['star'] = intval($val['star']);
			$returninfo[$key] = $val;
			$starcount += intval($val['star']);
			$count ++;
		}

		$stara = (($starcount*20)/$count)."%";
		$return = array('error' => 0, 'info' => array("data"=>$returninfo,"sorceinfo"=>array("count"=>$count,"star"=>$stara)));
		$this->ajaxReturn($return);
	}
	/**
	 * 获取详情
	 */
	public function commentinfo(){
		$param = I('params');

		//组装查询条件
		$id = I('data.id', '', '/^\d+$/', $param);

		$com = D('Comment')->commentList(array('id' => $id));
		$returninfo = array();
		foreach($com as $key=>$val){
			$val['star'] = intval($val['star']);
			$returninfo[$key] = $val;
		}
		$this->ajaxReturn($this->result($com));
	}
	/**
	 * 查询回复列表
	 * @author 刘新
	 */
	public function replyList()
	{
		$param = I('params');
	
		//组装查询条件
		$parentId = I('data.comment_id', '', '/^\d+$/', $param);
		if(!$parentId) $this->ajaxReturn(array('error' => 1, 'info' => '参数错误'));
		$option['page'] = I('data.page', '', '/^\d+$/', $param) ? $param['page'] : 1;
		$option['size'] = I('data.size', '', '/^\d+$/', $param) ? $param['size'] : 5;
	
		//查询
		$com = D('Comment')->commentList(array('parent_id' => $parentId), $option);
		$this->ajaxReturn($this->successResult($com));
	}
}